﻿using System;
using System.Collections.ObjectModel;
using Unknown.Framework.General;
using Unknown.Example.Access.Personnel;
using Unknown.Example.Entity.Personnel;

namespace Unknown.Example.Business.Personnel
{
	/// <summary>
	/// 职员业务抽象类
	/// </summary>
	public abstract class EmployeeBusiness
	{
        /// <summary>
        /// 是否存在引用
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual bool HasReference(Employee entity)
        {
            return true;
        }

        /// <summary>
        /// 安全新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual SafeResult<Employee> ExecuteSafeInsert(Employee entity)
        {
            SafeResult<Employee> result = new SafeResult<Employee>();

            result.Result = null;
            result.Execute = false;

			if (entity != null)
			{
				Employee current = this.ExecuteSelectUnique(entity);
				if (current == null)
				{
					result.Result = this.ExecuteInsert(entity);
					result.Execute = true;
				}
			}

            return result;
        }

        /// <summary>
        /// 安全修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual SafeResult<int> ExecuteSafeUpdate(Employee entity)
        {
            SafeResult<int> result = new SafeResult<int>();

            result.Result = 0;
            result.Execute = false;

			if (entity != null)
			{
				Employee current = this.ExecuteSelectUnique(entity);
				if ((current == null) || ((current != null) && (current.Pkid == entity.Pkid)))
				{
					result.Result = this.ExecuteUpdate(entity);
					result.Execute = true;
				}
			}

            return result;
        }

        /// <summary>
        /// 安全删除
        /// </summary>
		/// <param name="pkid">标识</param>
        /// <returns>结果</returns>
        public virtual SafeResult<int> ExecuteSafeDelete(string pkid)
        {
            SafeResult<int> result = new SafeResult<int>();

            result.Result = 0;
            result.Execute = false;

            Employee current = this.ExecuteSelect(pkid);
            bool has = this.HasReference(current);
            if (!has)
            {
                result.Result = ExecuteDelete(pkid);
                result.Execute = true;
            }

            return result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual Employee ExecuteInsert(Employee entity)
        {
            Employee result = null;

            if (entity != null)
            {
                EmployeeAccessCustom access = new EmployeeAccessCustom();
                result = access.ExecuteInsert(entity);
            }

            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual int ExecuteUpdate(Employee entity)
        {
            int result = 0;

            if (entity != null)
            {
                EmployeeAccessCustom access = new EmployeeAccessCustom();
                result = access.ExecuteUpdate(entity);
            }

            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
		/// <param name="pkid">标识</param>
        /// <returns>结果</returns>
        public virtual int ExecuteDelete(string pkid)
        {
            int result = 0;

            EmployeeAccessCustom access = new EmployeeAccessCustom();
            result = access.ExecuteDelete(pkid);

            return result;
        }

		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="first">开始行</param>
		/// <param name="last">结束行</param>
		/// <returns>结果</returns>
		public virtual Collection<Employee> ExecuteSelect(int first, int last)
		{
			Collection<Employee> results = null;

			EmployeeAccessCustom access = new EmployeeAccessCustom();
			results = access.ExecuteSelect(first, last);

			return results;
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <returns>结果</returns>
		public virtual Collection<Employee> ExecuteSelect()
		{
			Collection<Employee> results = null;

			results = this.ExecuteSelect(1, int.MaxValue);

			return results;
		}

        /// <summary>
        /// 查询
        /// </summary>
		/// <param name="pkid">标识</param>
        /// <returns>结果</returns>
        public virtual Employee ExecuteSelect(string pkid)
        {
            Employee result = null;
			
            EmployeeAccessCustom access = new EmployeeAccessCustom();
            result = access.ExecuteSelect(pkid);

            return result;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual Employee ExecuteSelectUnique(Employee entity)
        {
            Employee result = null;

            if (entity != null)
            {
				EmployeeAccessCustom access = new EmployeeAccessCustom();
                result = access.ExecuteSelectUnique(entity);
            }

            return result;
        }
	}
}
